<template>
  <div class="login-container">
    <video
      src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4"
      autoplay
      loop
      muted
      style="width: 100%; height: 100%; object-fit: fill"
    />
    <div class="login-box">
      <div class="logo">
        <img src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png" alt="">
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="account">
          <img src="" class="icon-img">
          <el-input v-model="form.account" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password">
          <img src="" class="icon-img">
          <el-input v-model="form.password" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码" />
          <i class="el-icon-view" @click="showPassword = !showPassword" />
        </el-form-item>
        <el-form-item prop="code">
          <img src="" class="icon-img">
          <el-input ref="inp" v-model="form.code" placeholder="请输入验证码" @keydown.enter.native="login" />
          <img :src="codeImage" alt="codeImage" class="code-image" @click="getCodeImage">
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login"><i v-if="isShow" class="el-icon-loading loading" />登录</el-button>
        </el-form-item>
      </el-form>
      <p>仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！</p>
    </div>
  </div>
</template>
<script>
import { getCode } from '@/api/user'
import { randomNum } from '@/utils/index'
export default {
  name: 'Login',
  data() {
    return {
      codeImage: null,
      showPassword: false,
      isShow: false,
      form: {
        account: 'demo',
        password: '888itcast.CN764%...',
        code: '',
        // 此处第一位数字表示是一个4位数,第二个数字表示是十进制
        key: randomNum(4, 10),
        bindAccount: '',
        bindPassword: '',
        singleAccount: '',
        singlePassword: '',
        tenant: '0000'
      },
      rules: {
        account: [
          { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
          { pattern: /^\w{4,12}$/, message: '请输入正确的用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { pattern: /^\S*(?=\S{6,})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*? ])\S*$/, message: '请输入正确的密码', trigger: 'blur' }
        ],
        key: [
          { required: true, message: '请输入验证码', trigger: ['blur', 'change'] },
          { pattern: /^\d{1,2}$/, message: '验证码不正确,请重新输入', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCodeImage()
    console.log(this.$refs.inp)
  },
  methods: {
    async getCodeImage() {
      const res = await getCode(this.form.key)
      // 获取验证码图片
      console.log(res)
      this.codeImage = URL.createObjectURL(res.data)
    },
    async handleLogin() {
      this.isShow = true
      await this.$store.dispatch('user/loginAction', this.form)
      this.isShow = false
      setTimeout(() => {
        this.$router.push('/')
      }, 500)
    },
    login() {
      this.$refs.form.validate(valid => {
        valid && this.handleLogin()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input__inner {
  height: 50px;
  padding-left: 48px;
  border: 1px solid #dcdfe6;
}
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;

.login-box {
  background-color: #fff;
  position: absolute;
  top: 45%;
  left: 45%;
  margin: -180px 0 0 -160px;
  width: 450px;
  height: 490px;
  padding: 36px 40px;
  // background: #fff;
  border-radius: 10px;
  .logo {
    height: 54px;
    text-align: center;
    margin-bottom: 30px;
    img {
      height: 100%;
    }
  }
  .el-form-item__content {
    position: relative;
    .icon-img {
      position: absolute;
      left: 13px;
      top: 14px;
      z-index: 2;
      width: 21px;
      height: 21px;
    }
    i {
      position: absolute;
      right: 12px;
      top: 15px;
      color: #c0c4cc;
      font-size: 20px;
      cursor: pointer;
      &:hover {
        color: #454545;
      }
    }
    .code-image {
      position: absolute;
      top: 11px;
      right: 13px;
      width: 71px;
      height: 32px;
    }
  }
  ::v-deep .el-button {
    position: relative;
    margin-top: 25px;
    width: 370px;
    height: 54px;
    font-size: 18px;
    font-weight: 600;
    border: none;
    background-color: #dd5339;
    .loading {
      position: absolute;
      top: 20px;
      left: -48px;
      color: #fff;
      font-size: 16px;
    }
  }
  p {
    font-size: 14px;
    color: #fd3333;
  }
}
}
</style>
